<!--
 * @Descripttion: 编辑轮播图
 * @Author: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @Date: 2020-09-28 15:24:24
 * @LastEditors: DXV-RGWU-TUFH-RFCY-IEGMYY
 * @LastEditTime: 2023-03-13 19:08:01
-->
<template>
  <div class="lb-system-banner-edit">
    <top-nav :title="navTitle" :isBack="true" />
    <div class="page-main">
      <el-form
        @submit.native.prevent
        :model="subForm"
        ref="subForm"
        :rules="subFormRules"
        label-width="130px"
      >
        <el-form-item label="图片" prop="img">
          <lb-cover
            :fileList="subForm.img"
            @selectedFiles="getCover"
          ></lb-cover>
          <lb-tool-tips>图片建议尺寸: 750 * 400</lb-tool-tips>
        </el-form-item>
        <el-form-item label="关联内容" prop="connect_type">
          <el-radio-group v-model="subForm.connect_type" @input="submitOne()">
            <el-radio :label="1">查看大图</el-radio>
            <el-radio :label="2">文章</el-radio>
            <el-radio :label="3">服务分类</el-radio>
            <el-radio :label="4">项目</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="" prop="type_id" v-if="subForm.connect_type === 2">
          <el-tag
            @click="toShowDialog"
            :type="subForm.type_id ? 'primary' : 'danger'"
            >{{ subForm.type_id ? subForm.type_title : `请选择文章` }}</el-tag
          >
        </el-form-item>
        <el-form-item label="" prop="type_id" v-if="subForm.connect_type === 3">
          <el-tag
            @click="toShowType"
            :type="subForm.type_id ? 'primary' : 'danger'"
            >{{ subForm.type_id ? subForm.type_title : `请选择服务分类` }}</el-tag
          >
        </el-form-item>
        <el-form-item label="" prop="type_id" v-if="subForm.connect_type === 4">
          <el-tag
            @click="toShowProject"
            :type="subForm.type_id ? 'primary' : 'danger'"
            >{{ subForm.type_id ? subForm.type_title : `请选择项目` }}</el-tag
          >
        </el-form-item>
        <el-form-item label="排序值" prop="top">
          <el-input-number
            class="lb-input-number"
            :min="0"
            :controls="false"
            v-model="subForm.top"
            placeholder="请输入排序值"
          ></el-input-number>
          <lb-tool-tips>值越大, 排序越靠前</lb-tool-tips>
        </el-form-item>
        <el-form-item>
          <lb-button type="primary" @click="submitForm" v-preventReClick>{{
            $t('action.submit')
          }}</lb-button>
          <lb-button @click="$router.back(-1)">{{
            $t('action.back')
          }}</lb-button>
        </el-form-item>
      </el-form>
    </div>

    <el-dialog
      title="选择文章"
      :visible.sync="showDialog"
      width="1000px"
      center
    >
      <el-form
        @submit.native.prevent
        :inline="true"
        :model="searchForm"
        ref="searchForm"
      >
        <el-form-item label="输入查询" prop="title">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入文章标题"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <lb-button
            size="medium"
            type="primary"
            icon="el-icon-search"
            style="margin-right: 5px"
            @click="getTableDataList(1)"
            >{{ $t('action.search') }}</lb-button
          >
          <lb-button
            size="medium"
            icon="el-icon-refresh-left"
            style="margin-right: 5px"
            @click="resetForm('searchForm')"
            >{{ $t('action.reset') }}</lb-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="tableData"
        ref="singleTable"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChange"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="title" label="文章标题"></el-table-column>
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showDialog = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="选择服务分类"
      :visible.sync="showType"
      width="1000px"
      center
    >
      <el-form
        @submit.native.prevent
        :inline="true"
        :model="searchForm"
        ref="searchForm"
      >
        <el-form-item label="输入查询" prop="title">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入分类标题"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <lb-button
            size="medium"
            type="primary"
            icon="el-icon-search"
            style="margin-right: 5px"
            @click="getTableDataListType(1)"
            >{{ $t('action.search') }}</lb-button
          >
          <lb-button
            size="medium"
            icon="el-icon-refresh-left"
            style="margin-right: 5px"
            @click="resetFormType('searchForm')"
            >{{ $t('action.reset') }}</lb-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="tableTypeData"
        ref="singleTable"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChangeType"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="title" label="名称"></el-table-column>
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showType = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirmType">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="选择项目"
      :visible.sync="showProject"
      width="1000px"
      center
    >
      <el-form
        @submit.native.prevent
        :inline="true"
        :model="searchForm"
        ref="searchForm"
      >
        <el-form-item label="输入查询" prop="title">
          <el-input
            v-model="searchForm.title"
            placeholder="请输入项目标题"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <lb-button
            size="medium"
            type="primary"
            icon="el-icon-search"
            style="margin-right: 5px"
            @click="getTableDataListProject(1)"
            >{{ $t('action.search') }}</lb-button
          >
          <lb-button
            size="medium"
            icon="el-icon-refresh-left"
            style="margin-right: 5px"
            @click="resetFormType('searchForm')"
            >{{ $t('action.reset') }}</lb-button
          >
        </el-form-item>
      </el-form>
      <el-table
        :data="tableProjectData"
        ref="singleTable"
        :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        @current-change="handleSelectionChangeType"
      >
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="title" label="名称"></el-table-column>
        <el-table-column prop="top" label="排序值"></el-table-column>
        <el-table-column prop="create_time" label="创建时间">
          <template slot-scope="scope">
            <p>{{ scope.row.create_time | handleTime(1) }}</p>
            <p>{{ scope.row.create_time | handleTime(2) }}</p>
          </template>
        </el-table-column>
      </el-table>
      <lb-page
        :batch="false"
        :page="searchForm.page"
        :pageSize="searchForm.limit"
        :total="total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      >
      </lb-page>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showProject = false">取 消</el-button>
        <el-button type="primary" @click="handleDialogConfirmProject">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import moment from 'moment'
export default {
  data () {
    let checkType = (rule, value, callback) => {
      let { connect_type: type } = this.subForm
      let { typeText } = this
      if (type !== 1 && !value) {
        let msg = `请选择${typeText[type]}`
        callback(msg)
      } else {
        callback()
      }
    }
    return {
      navTitle: '',
      typeText: {
        1: '查看大图',
        2: '文章'
      },
      articleOptions: [],
      subForm: {
        img: '',
        connect_type: 1,
        type_id: '',
        type_title: '',
        top: 0
      },
      subFormRules: {
        img: { required: true, type: 'array', message: '请上传图片', trigger: 'blur' },
        connect_type: { required: true, type: 'number', message: '请选择关联内容', trigger: 'blur' },
        type_id: { required: true, validator: checkType, trigger: 'blur' },
        top: { required: true, type: 'number', message: '请输入排序值', trigger: 'blur' }
      },
      showDialog: false,
      showType: false,
      showProject:false,
      loading: false,
      searchForm: {
        page: 1,
        limit: 10,
        title: '',
        status: 1,
        level: 1,
				type: 1
      },
      tableData: [],
      tableTypeData:[],
      tableProjectData:[],
      total: 0
    }
  },
  created () {
    let { id } = this.$route.query
    if (id) {
      this.subForm.id = id
      this.getDetail(id)
    }
    this.navTitle = this.$t(id ? 'menu.ServiceBannerEdit' : 'menu.ServiceBannerAdd')
  },
  methods: {
    submitOne(){
      console.log('选择');
      this.subForm.type_id = ''
      this.subForm.type_title = ''
    },
    /**
     * @method 获取图片
     */
    getCover (img) {
      this.subForm.img = img
    },
    async getDetail (id) {
      let { code, data } = await this.$api.service.bannerInfo({ id })
      if (code !== 200) return
      console.log(data,'-----------------');
      data.img = [{ url: data.img }]
      data.link = data.link * 1 > 0 ? data.link * 1 : ''
      for (let key in this.subForm) {
        this.subForm[key] = data[key]
      }
      this.subForm.type_title = data.type.title
      console.log();
    },
    resetForm (form) {
      this.$refs[form].resetFields()
      this.getTableDataList(1)
    },
    resetFormType (form) {
      this.$refs[form].resetFields()
      this.getTableDataListType(1)
    },
    handleSizeChange (val) {
      this.searchForm.limit = val
      this.handleCurrentChange(1)
    },
    handleCurrentChange (val) {
      this.searchForm.page = val
      this.getTableDataList()
      this.getTableDataListType()
      this.getTableDataListProject()
    },
    async getTableDataListType (flag) {
      if (flag) this.searchForm.page = 1
      this.tableTypeData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { code, data } = await this.$api.service.cateListType(searchForm)
      this.loading = false
      if (code !== 200) return
      console.log(data,'----------------');
      this.tableTypeData = data.data
      this.total = data.total
    },
    async getTableDataList (flag) {
      if (flag) this.searchForm.page = 1
      this.tableData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { code, data } = await this.$api.market.articleList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableData = data.data
      this.total = data.total
    },
    async getTableDataListProject (flag) {
      if (flag) this.searchForm.page = 1
      this.tableProjectData = []
      this.loading = true
      let searchForm = JSON.parse(JSON.stringify(this.searchForm))
      let { code, data } = await this.$api.service.serviceList(searchForm)
      this.loading = false
      if (code !== 200) return
      this.tableProjectData = data.data
      this.total = data.total
    },
    async toShowDialog () {
      this.searchForm.title = ''
      await this.getTableDataList(1)
      this.showDialog = !this.showDialog
    },
    async toShowType () {
      this.searchForm.title = ''
      await this.getTableDataListType(1)
      this.showType = !this.showType
    },
    async toShowProject () {
      this.searchForm.title = ''
      await this.getTableDataListProject(1)
      this.showProject = !this.showProject
    },
    handleSelectionChange (val) {
      val = JSON.parse(JSON.stringify(val))
      this.currentRow = val
    },
    handleSelectionChangeType (val) {
      val = JSON.parse(JSON.stringify(val))
      this.currentRow = val
    },
    handleDialogConfirm () {
      if (this.currentRow === null || !this.currentRow.id) {
        this.$message.error(`请选择文章`)
        return
      }
      let { id = 0, title = '' } = this.currentRow
      this.subForm.type_id = id
      this.subForm.type_title = title
      this.showDialog = false
    },
    handleDialogConfirmType () {
      if (this.currentRow === null || !this.currentRow.id) {
        this.$message.error(`请选择服务分类`)
        return
      }
      let { id = 0, title = '' } = this.currentRow
      this.subForm.type_id = id
      this.subForm.type_title = title
      this.showType = false
    },
    handleDialogConfirmProject () {
      if (this.currentRow === null || !this.currentRow.id) {
        this.$message.error(`请选择项目`)
        return
      }
      let { id = 0, title = '' } = this.currentRow
      this.subForm.type_id = id
      this.subForm.type_title = title
      this.showProject = false
    },
    /**
     * @method 新增/编辑
     */
    submitForm () {
      let flag = true
      this.$refs['subForm'].validate(valid => {
        if (!valid) flag = false
      })
      if (flag) {
        let subForm = JSON.parse(JSON.stringify(this.subForm))
        subForm.img = subForm.img[0].url
        delete subForm.type_title
        let modelMethod = subForm.id ? 'bannerUpdate' : 'bannerAdd'
        this.$api.service[modelMethod](subForm).then(res => {
          if (res.code === 200) {
            this.$message.success(this.$t(subForm.id ? 'tips.successRev' : 'tips.successSub'))
            this.$router.back(-1)
          }
        })
      }
    }
  },
  filters: {
    handleTime (val, type) {
      let time = type === 1 ? moment(val * 1000).format('YYYY-MM-DD') : type === 2 ? moment(val * 1000).format('HH:mm:ss') : moment(val * 1000).format('YYYY-MM-DD HH:mm:ss')
      return time
    }
  }
}
</script>

<style lang="scss" scoped>
.lb-system-banner-edit {
  width: 100%;
  .el-form {
    width: 100%;
    .el-form-item {
      margin-bottom: 24px;
      .el-select,
      .el-input-number,
      .el-input {
        width: 300px;
      }
    }
    .last-form-item {
      margin-top: 30px;
    }
    .item-tips {
      margin-left: 120px;
      margin-bottom: 24px;
      color: #999999;
    }
  }
}
</style>
